import Vue from 'vue';
import router from '../router';
import _axios from './config.js';
import caches from "util/caches.js";
import utils from 'util/utils.js';
import constant from 'util/constant.js';
import {
	Message
} from 'element-ui';


_axios.interceptors.request.use(
	config => {
		config.headers["jwttoken"] = caches.token.get();
		return config;
	}, error => {
		utils.msg.exception(error);
		return Promise.reject(error);
	}
);


// Add a response interceptor
_axios.interceptors.response.use(response => {
	return response.data;
}, error => {
	utils.msg.exception(error);
	return Promise.reject(error);
});
Plugin.install = function(Vue, options) {
	Vue.axios = _axios;
	window.axios = _axios;
	Object.defineProperties(Vue.prototype, {
		axios: {
			get() {
				return _axios;
			}
		},
		$axios: {
			get() {
				return _axios;
			}
		},
	});
};

Vue.use(Plugin)
let ApiUtils = {
	sendLoadingAjax(url, data, successCallback, failCallback, exceptionCallback) {
		let messageObj = Message.info({
			message: "加载中",
			iconClass: "el-icon-loading",
			duration: 0
		});
		_axios({
			method: "post",
			url,
			data
		}).then(response => {
			messageObj.close();
			ApiUtils.baseCallBack(url, data, response, successCallback, failCallback, exceptionCallback);
		}, error => {
			messageObj.close();
		});
	},
	sendAjax(url, data, successCallback, failCallback, exceptionCallback) {
		_axios({
			method: "post",
			url,
			data
		}).then(response => {
			ApiUtils.baseCallBack(url, data, response, successCallback, failCallback, exceptionCallback);
		});
	},
	baseCallBack(url, data, response, successCallback, failCallback, exceptionCallback) {
		//登录失效
		if (response.code == constant.response.code.nologin || response.code == constant.response.code.badtoken) {
			utils.msg.warning("未登录!")
			setTimeout(() => {
				router.replace('/login');
			}, 1000);
			//刷新token
		} else if (response.code == constant.response.code.flusntoken) {
			//更新token
			caches.token.set(response.data);
			_axios.interceptors.request.use(
				function(config) {
					config.headers["jwttoken"] = caches.token.get();
					// Do something before request is sent
					return config;
				},
				function(error) {
					// Do something with request error
					return Promise.reject(error);
				}
			);
			ApiUtils.sendAjax(url, data, successCallback, failCallback, exceptionCallback);
			//请求成功
		} else if (response.code == constant.response.code.success) {
			if (successCallback) {
				successCallback(response);
			} else {
				utils.msg.success(response.msg);
			}
			//请求失败
		} else if (response.code == constant.response.code.fail) {
			if (failCallback) {
				failCallback(response);
			} else {
				utils.msg.warning(response.msg);
			}
			//后台异常
		} else if (response.code == constant.response.code.exception) {
			if (exceptionCallback) {
				exceptionCallback(response);
			} else if (failCallback) {
				failCallback(response);
			} else {
				utils.msg.warning(response.msg);
			}
			//没有权限
		} else if (response.code == constant.response.code.noauth) {
			utils.msg.warning("没有操作权限!")
		}
	},
}

export {
	ApiUtils,
	_axios
};
